package cn.tedu.tea.service;

import cn.tedu.tea.domain.OrderLog;
import cn.tedu.tea.domain.param.OrderLogAddNewDTO;
import cn.tedu.tea.domain.param.OrderLogUpdateDTO;
import cn.tedu.tea.domain.vo.OrderLogListVO;
import cn.tedu.tea.domain.vo.OrderLogStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.OrderLogMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderLogServiceImpl implements IOrderLogService {

    @Autowired
    private OrderLogMapper orderLogMapper;

    @Override
    public void addNew(OrderLogAddNewDTO orderLogAddNewDTO) {
        log.debug("开始处理【添加订单状态】的业务，参数： {}", orderLogAddNewDTO);
        int count = orderLogMapper.countByOrderNo(orderLogAddNewDTO.getOrderNo());
        if (count > 0){
            String message = "添加订单状态失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderLog orderLog = new OrderLog();
        BeanUtils.copyProperties(orderLogAddNewDTO, orderLog);
        int rows = orderLogMapper.insert(orderLog);
        if (rows != 1){
            String message = "添加订单状态失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除订单状态】的业务，参数： {}", id);
        OrderLogStandardVO orderLogStandard = orderLogMapper.getStandardById(id);
        if (orderLogStandard == null){
            String message = "删除订单状态失败，要删除的订单状态不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = orderLogMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单状态失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除订单状态】的业务，参数： {}", ids);
        int rows = orderLogMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除订单状态失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(OrderLogUpdateDTO orderLogUpdateDTO) {
        log.debug("开始处理【修改订单状态信息】的业务，参数：{}", orderLogUpdateDTO);
        OrderLogStandardVO orderLogStandard = orderLogMapper.getStandardById(orderLogUpdateDTO.getId());
        if (orderLogStandard == null){
            String message = "修改订单状态失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = orderLogMapper.countByOrderNoAndNotId(orderLogUpdateDTO.getOrderNo(), orderLogUpdateDTO.getId());
        if (count > 0){
            String message = "修改订单状态失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderLog orderLog = new OrderLog();
        BeanUtils.copyProperties(orderLogUpdateDTO, orderLog);
        int rows = orderLogMapper.update(orderLog);
        if (rows != 1){
            String message = "修改订单状态失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public OrderLogStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询订单状态详情】的业务，参数： {}", id);
        OrderLogStandardVO orderLogStandard = orderLogMapper.getStandardById(id);
        if (orderLogStandard == null){
            String message = "查询订单状态详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return orderLogStandard;
    }

    @Override
    public List<OrderLogListVO> list() {
        log.debug("开始处理【查询订单状态列表】的业务，无参数");
        List<OrderLogListVO> list = orderLogMapper.list();
        return list;
    }
    
}
